
var aRootData=JSON.parse(localStorage.getItem('rootdata'))||[
    { name:'龙龙',root:'高级权限',username:'L020050612',password:'123456',index:0},
    { name:'泽泽',root:'普通权限',username:'Z020070414',password:'123456',index:1},
    { name:'军军',root:'高级权限',username:'J020020512',password:'123456',index:2},
    { name:'世祥',root:'高级权限',username:'S020070424',password:'123456',index:3},
    { name:'涵涵',root:'高级权限',username:'H020170812',password:'123456',index:4},
    { name:'锦涵',root:'高级权限',username:'J020140917',password:'123456',index:5},
    { name:'维强',root:'普通权限',username:'W020151112',password:'123456',index:6},
];
;

localStorage.setItem('rootdata',JSON.stringify(aRootData));

var dataIndex=0;
if (aRootData.length){
    var dataIndex=aRootData[aRootData.length-1].index+1;

}



function checkFn() {

    $('#checkAllBtn').click(function () {
        $('table tr td input[type=checkbox]').prop('checked', true);


    });
    $('#InvertBtn').click(function () {
        $('table tr td input[type=checkbox]').each(function () {
            $(this).prop('checked', !$(this).prop('checked'));
        });


    });
    $('#delBtn').click(function () {

        var flag=false;
        $('table tr td input[type=checkbox]').each(function () {


            if ($(this).prop('checked')) {

                for (var j=0;j<aRootData.length;j++){
                    if (aRootData[j].index==$(this).attr('dataIndex')) {

                        aRootData.splice( j,1);


                        flag=true;
                    }
                }







            }

        });


        if (flag){
            creatPageN();
            localStorage.setItem('rootdata',JSON.stringify(aRootData));
            $('.page-box').css({
                display:'block'
            });
            getThisPageData(n);
            console.log(n);
            $('#myPageBox a').eq(n-1).attr('class','active').siblings('a').attr('class','');
            if ($('#tabContent').html()==''){
                $('#myPageBox a') .attr('class','');
            }

            myAlert('删除成功')

        }else {
            myAlert('未选中任何内容删除失败')


        }
    });
}
checkFn();
//全选反选删除

var oAddBtn=$('.root-addUser');
var oZdc=$('.zdc');
var oAddpage=$('.alert-addUser');
var oNoneAddPage=$('.alert-addUser-exit');
var oSubmitAdd=$('.submitAdd');
var oAddName=$('#addName');
var oAddUserName=$('#addUserName');
var oAddPassword=$('#addPassword');
var oAddRoot=$('#addRoot');
var oRootTab=$('#root-tab');
oAddBtn.click(function () {



    oZdc.css(
        {
            display:'block',

        }
    );

    oAddpage.css(
        {
            display:'block',

        }
    );
    setTimeout(function(){
        oAddpage.css(
            {


                top:'20%',
                opacity:'1'
            }
        )
    },100);
});
oNoneAddPage.click(
    function () {

        oAddpage.css(
            {

                top:'0%',
                opacity:'0'
            }
        );
        oZdc.css(
            {
                display:'none'
            }
        );
        setTimeout(function () {
            oAddpage.css(
                {
                    display:'none'
                }
            );
        },300)
    }
);




if (aRootData.length){
    getThisPageData(1);
}

oSubmitAdd.click(function () {

    var nameFlag=false;
    var userNameFlag=false;
    var passWord=false;
    if (oAddName.val().trim()){
        nameFlag=true;
    }
    if (oAddUserName.val().trim()){
        userNameFlag=true
    }
    if (oAddPassword.val().trim()){
        passWord=true
    }
    if (nameFlag&&userNameFlag&&passWord) {
        $('.page-box').css({
            display:'block'
        });

        aRootData.push(
            {
                name:oAddName.val(),
                root:oAddRoot.val(),
                username:oAddUserName.val(),
                password:oAddPassword.val(),
                index:dataIndex++
            }
        );
        localStorage.setItem('rootdata',JSON.stringify(aRootData));
        myAlert('添加成功');
        thisPage=Math.ceil((aRootData.length)/5);
        getThisPageData(thisPage);
        creatPageN();
        n=thisPage
        $('#myPageBox a').eq(n-1).attr('class','active').siblings('a').attr('class','')

    }else {
        myAlert('不能有空')
    }


});


function readRootData(){
    $('#tabContent').html('');
    for (var i=0;i<aPage.length;i++){
        $('#tabContent').append(
            $('<tr>' +
                '<td> <input type="checkbox" dataIndex='+aPage[i].index+'> </td>' +
                ' <td class="TDnameData">'+aPage[i].name+'</td>' +
                '<td class="TDrootData">'+aPage[i].root+'</td> ' +
                '<td class="TDuserNameData">'+aPage[i].username+'</td>' +
                '<td class="TDpassword">'+aPage[i].password+'</td> ' +
                '<td></td> ' +
                '<td><a class="setRootData" href="#">设置</a></td>' +
                '</tr>')
        );
    }

}


oAddpage.mousedown(function () {
    var disX= event.pageX-oAddpage.offset().left;
    var disY= event.pageY-oAddpage.offset().top;

    $(document).mousemove(function () {

        var tempX=event.pageX-disX;
        var tempY=event.pageY-disY;

        oAddpage.css(
            {
                margin:0,
                left:tempX+'px',
                top:tempY+'px'
            }
        )
        return false
    });
    $(document).mouseup(
        function () {
            $(document).unbind('mousemove')
        }
    )

});
function myAlert(setVal) {

    var getMyalert=$('#myAlertEle');
    getMyalert.css({
        zIndex:'99'
    });
    getMyalert.html(setVal);
    getMyalert.css(
        {
            display:'block',

        }
    );
    var timer1=setTimeout(function () {
        clearInterval(timer1);

        getMyalert.css(
            {
                top:'20%',
                opacity:'1'
            }
        )
    },100);

    var timer2=setTimeout(

        function () {
            clearInterval(timer2)

            getMyalert.css(
                {
                    top:'0%',
                    opacity:'0'
                }
            )
        },1000
    );
    var timer3=setTimeout(
        function () {
            clearInterval(timer3);
            getMyalert.css(
                {
                    display:'none'
                }
            )
        },1300
    )


};
var getIndex;
$('#tabContent').on('click','.setRootData',function () {
    oZdc.css(
        {
            display:'block',

        }
    );

    $('#mySetAlertEle').css(
        {
            display:'block',

        }
    );
    setTimeout(function(){
        $('#mySetAlertEle').css(
            {


                top:'20%',
                opacity:'1'
            }
        )
    },100);
    var setNameVal=$(this).parents('tr').children('.TDnameData').html();
    var setRootVal=$(this).parents('tr').children('.TDrootData').html();
    var setUsernameVal=$(this).parents('tr').children('.TDuserNameData').html();
    var setPasswordVal=$(this).parents('tr').children('.TDpassword').html();
    getIndex=$(this).parents('tr').children().children('input[type=checkbox]').attr('dataindex');

    $('#setName').val(setNameVal);
    $('#setRoot').val(setRootVal);
    $('#setUsername').val(setUsernameVal);
    $('#setPassword').val(setPasswordVal);
    for (var i=0;i<aRootData.length;i++){
        if (aRootData[i].index==getIndex){

            if (aRootData[i].addGGroot==1){
                $('#gg').prop('checked',true)
            } else {
                $('#gg').prop('checked',false)

            }

            if (aRootData[i].addJYroot==1){
                $('#jy').prop('checked',true)
            } else {
                $('#jy').prop('checked',false)

            }
            if (aRootData[i].addBJroot==1){
                $('#bj').prop('checked',true)
            } else {
                $('#bj').prop('checked',false)

            }
            if (aRootData[i].addXSroot==1){
                $('#xs').prop('checked',true)
            } else {
                $('#xs').prop('checked',false)

            }
            if (aRootData[i].addHQroot==1){
                $('#hq').prop('checked',true)
            } else {
                $('#hq').prop('checked',false)

            }
            if (aRootData[i].addBMroot==1){
                $('#bm').prop('checked',true)
            } else {
                $('#bm').prop('checked',false)

            }



        }


    }

});
$('.submitSet ').click(function () {
    $('.page-box').css({
        display:'block'
    });
    var addGGroot=0;
    var addJYroot=0;
    var addBJroot=0;
    var addXSroot=0;

    var addBMroot=0;

    if ($('#gg').prop('checked')){
        addGGroot=1
    };
    if ($('#jy').prop('checked')){
        addJYroot=1
    };
    if ($('#bj').prop('checked')){
        addBJroot=1
    };
    if ($('#xs').prop('checked')){
        addXSroot=1
    };

    if ($('#bm').prop('checked')){
        addBMroot=1
    };
    for (var i=0;i<aRootData.length;i++){

        if (aRootData[i].index==getIndex){

            aRootData[i]= {
                name:  $('#setName').val(),
                root:$('#setRoot').val(),
                username:$('#setUsername').val(),
                password:$('#setPassword').val(),
                index:getIndex,
                headsrc:aRootData[i].headsrc,
                addGGroot:addGGroot,
                addJYroot:addJYroot,
                addBJroot:addBJroot,
                addXSroot:addXSroot,

                addBMroot:addBMroot,

            }


        }



    };

    myAlert('修改成功');




    getThisPageData(n ||1);
    localStorage.setItem('rootdata',JSON.stringify(aRootData))


});
$('#mySetAlertEle .alert-setUser-exit').click( function () {

    $('#mySetAlertEle').css(
        {

            top:'0%',
            opacity:'0'
        }
    );
    oZdc.css(
        {
            display:'none'
        }
    );
    setTimeout(function () {
        $('#mySetAlertEle').css(
            {
                display:'none'
            }
        );
    },300)
});
var oSearchInput=document.getElementById('searchInput')

oSearchInput.oninput=function () {
    $('#tabContent').html('');
    $('.page-box').css({
        display:'none'
    });
    var inputVlue=oSearchInput.value;

    for (var i=0;i<aRootData.length;i++){

        var temp=JSON.stringify(aRootData[i]);

        var temp2=JSON.parse(temp);
        temp2.index='';


        var temp3=temp2.name+temp2.username+temp2.root+temp2.password;


        if (temp3.indexOf(inputVlue)!=-1){
            var lastTemp=JSON.parse(temp);

            $('#tabContent').append(

                $('<tr>' +
                    '<td> <input type="checkbox" dataIndex='+lastTemp.index+'> </td>' +
                    ' <td class="TDnameData">'+lastTemp.name+'</td>' +
                    '<td class="TDrootData">'+lastTemp.root+'</td> ' +
                    '<td class="TDuserNameData">'+lastTemp.username+'</td>' +
                    '<td class="TDpassword">'+lastTemp.password+'</td> ' +
                    '<td></td> ' +
                    '<td><a class="setRootData" href="#">设置</a></td>' +
                    '</tr>')

            )
        }


    }
    if (inputVlue==''){
        $('.page-box').css({
            display:'block'
        });
        getThisPageData(1);
        $('#myPageBox').children('a').eq(0).attr('class','active').siblings('a').attr('class','')
    }


};
var aPage=[];
function getThisPageData(pageN) {

    aPage=aRootData.slice((pageN-1)*5,pageN*5);
    readRootData()

}
function creatPageN() {
    $('#myPageBox').html('');
    var n=Math.ceil((aRootData.length)/5);

    for (var i=1;i<=n;i++){

        if (i==1){
            $('#myPageBox').append('<a href="#" class="nav-ul active">'+i+'</a>');
        }else {
            $('#myPageBox').append('<a href="#" class="nav-ul">'+i+'</a>');

        }
    }
}
creatPageN();

var thisPage=Math.ceil((aRootData.length)/5);
console.log(thisPage);
var n=1;
$('#myPageBox').on('click','a',function () {
    n=$(this).text();
    getThisPageData(n);
    $(this).attr('class','active').siblings('a').attr('class','')
});
$('#nextPage').click(function () {


    var temp=Math.ceil((aRootData.length)/5);
    console.log(n);
    if (n!=temp){
        n++;
    }else {
        myAlert('以是最后一页')
    }
    getThisPageData(n);
    $('#myPageBox a').eq(n-1).attr('class','active').siblings('a').attr('class','')

});
$('#prevPage').click(function () {

    console.log(n);
    if (n!=1){
        n--;
    }else {
        myAlert('以是第一页')
    }
    getThisPageData(n);
    $('#myPageBox a').eq(n-1).attr('class','active').siblings('a').attr('class','')
});